
// DrawDotMatrixDoc.cpp : implementation of the CDrawDotMatrixDoc class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "DrawDotMatrix.h"
#endif

#include "DrawDotMatrixDoc.h"
#include "ChildFrm.h"
#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CDrawDotMatrixDoc

IMPLEMENT_DYNCREATE(CDrawDotMatrixDoc, CDocument)

BEGIN_MESSAGE_MAP(CDrawDotMatrixDoc, CDocument)
END_MESSAGE_MAP()


// CDrawDotMatrixDoc construction/destruction

CDrawDotMatrixDoc::CDrawDotMatrixDoc()
{
	// TODO: add one-time construction code here
	m_nWidth = 1024;
	m_nHeight = 100;
	GenerateImage();
}

CDrawDotMatrixDoc::~CDrawDotMatrixDoc()
{
	//if(m_pImg1)
	//{
	//	delete[] m_pImg1;
	//	m_pImg1 = NULL;
	//}
	//if(m_pImg2)
	//{
	//	delete m_pImg2;
	//	m_pImg2 = NULL;
	//}
	//if(m_pImg3)
	//{
	//	delete m_pImg3;
	//	m_pImg3 = NULL;
	//}
	//if(m_pImg4)
	//{
	//	delete m_pImg4;
	//	m_pImg4 = NULL;
	//}
}

BOOL CDrawDotMatrixDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	// Create view 1 with the current template frame
	CFrameWnd* pFrame = NULL;
	CRuntimeClass* pRuntimeViewClass = RUNTIME_CLASS(CMiniView);
	pFrame = CreateNewView(pRuntimeViewClass);
	//CSize size;
	//size.cx = GetSystemMetrics(SM_CXSCREEN);
	//size.cy = GetSystemMetrics(SM_CYSCREEN);
	m_pMiniView1 = (CMiniView*)pFrame->GetActiveView();
	m_pMiniView1->m_szText = "Image 1";
	m_pMiniView1->m_pImg = m_pImg1;

	// Create view 2 with the current template frame
	pFrame = CreateNewView(pRuntimeViewClass);
	m_pMiniView2 = (CMiniView*)pFrame->GetActiveView();
	m_pMiniView2->m_szText = "Image 2";
	m_pMiniView2->m_pImg = m_pImg2;

	// Create view 3 with the current template frame
	pFrame = CreateNewView(pRuntimeViewClass);
	m_pMiniView3 = (CMiniView*)pFrame->GetActiveView();
	m_pMiniView3->m_szText = "Image 3";
	m_pMiniView3->m_pImg = m_pImg3;

	// Create view 4 with the current template frame
	pFrame = CreateNewView(pRuntimeViewClass);
	m_pMiniView4 = (CMiniView*)pFrame->GetActiveView();
	m_pMiniView4->m_szText = "Image 4";
	m_pMiniView4->m_pImg = m_pImg4;

	return TRUE;
}




// CDrawDotMatrixDoc serialization

void CDrawDotMatrixDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

#ifdef SHARED_HANDLERS

// Support for thumbnails
void CDrawDotMatrixDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// Modify this code to draw the document's data
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CDrawDotMatrixDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// Set search contents from document's data. 
	// The content parts should be separated by ";"

	// For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
	SetSearchContent(strSearchContent);
}

void CDrawDotMatrixDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CDrawDotMatrixDoc diagnostics

#ifdef _DEBUG
void CDrawDotMatrixDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CDrawDotMatrixDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}

void CDrawDotMatrixDoc::GenerateImage()
{
	int y=0, x=0;

	// generate some pattern for both images. 8 bit per pixel
	m_nSize = m_nWidth * m_nHeight;
	
	m_pImg = new BYTE[m_nSize];
	m_pImg1 = new BYTE[m_nSize];
	m_pImg2 = new BYTE[m_nSize];
	m_pImg3 = new BYTE[m_nSize];
	m_pImg4 = new BYTE[m_nSize];

	// Black is 0, white is 255
	memset (m_pImg, 0, m_nSize);		// Set blank image
	// Black is 0, white is 255
	memset (m_pImg1, 255, m_nSize);		// Set blank image
	for (y=0; y<m_nHeight; y+=4)
	{
		if(y < 25)
		{
			for (x=0; x<m_nWidth; x+=4)
			{
				m_pImg1[x+(y*m_nWidth)] = 0;
			}
		}
		else
		{
			for (x=0; x<m_nWidth; x+=4)
			{
				m_pImg1[x+(y*m_nWidth)] = 0;
			}
		}
	}
	// Generate 2st image
	memset (m_pImg2, 255, m_nSize);		// Set vertical line image
	for (y=0; y<m_nHeight; y+=10)
	{
		for (x=0; x<m_nWidth; x+=10)
		{
			m_pImg2[x+(y*m_nWidth)] = 0;
		}
	}

	// Generate 3st image
	memset (m_pImg3, 255, m_nSize);		// Set vertical line image
	for (y=0; y<m_nHeight; y+=10)
	{
		for (x=0; x<m_nWidth; x+=10)
		{
			m_pImg3[x+(y*m_nWidth)] = 0;
		}
	}

	// Generate 4st image
	memset (m_pImg4, 255, m_nSize);		// Set vertical line image
	for (y=0; y<m_nHeight; y+=10)
	{
		for (x=0; x<m_nWidth; x+=10)
		{
			m_pImg4[x+(y*m_nWidth)] = 0;
		}
	}
}

CFrameWnd* CDrawDotMatrixDoc::CreateNewView(CRuntimeClass* pViewClass)
{
	POSITION pos = GetFirstViewPosition();
	CMDIChildWnd* pActiveChild = (CMDIChildWnd*)GetNextView(pos);

	CDocument* pDocument = this;
	CDocTemplate* pTemplate = pDocument->GetDocTemplate();
	ASSERT_VALID(pTemplate);

	CRuntimeClass* pRuntimeViewClass = pViewClass;
	CCreateContext newContext;
	newContext.m_pNewViewClass = pRuntimeViewClass;
	newContext.m_pNewDocTemplate = pTemplate;
	newContext.m_pLastView = NULL;
	newContext.m_pCurrentFrame = pActiveChild;
	newContext.m_pCurrentDoc = this;

	CRuntimeClass* pFrameClass = RUNTIME_CLASS(CChildFrame);
	CObject* pObject = pFrameClass->CreateObject();
	if (pFrameClass == NULL)
	{
		TRACE0("Error: you must override CDocTemplate::CreateNewFrame.\n");
		ASSERT(FALSE);
		return NULL;
	}
	CFrameWnd* pFrame = (CFrameWnd*)pObject;
	if (pFrame == NULL)
	{
		TRACE1("Warning: Dynamic create of frame %hs failed.\n",
			pFrameClass->m_lpszClassName);
		return NULL;
	}
	ASSERT_KINDOF(CFrameWnd, pFrame);

	// create new from resource
	if (!pFrame->LoadFrame(IDR_MAINFRAME,
			WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,   // default frame styles
			NULL, &newContext))
	{
		TRACE0("Warning: CDocTemplate couldn't create a frame.\n");
		// frame will be deleted in PostNcDestroy cleanup
		return NULL;
	}

	// it worked !
	//////////////////////////////////////////////////////
	pTemplate->InitialUpdateFrame(pFrame, pDocument);

	// Get the active view attached to the active MDI child
	// window.
	//CView *pView = (CView *) pFrame->GetActiveView();
	return pFrame;
}


#endif //_DEBUG


// CDrawDotMatrixDoc commands
